/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} k
 * @return {ListNode}
 */
let list=require('./lianbiao');
var rotateRight = function(head, k) {
    if(head==null || head.next==null || k==0){
        return head;
    }
    let n=1;
    let node=head;
    while(node.next){
        node=node.next;
        n++;
    }
    let m=n-k%n;
    if(m==n){
        return head;
    }
    node.next=head;
    while(m>0){
        node=node.next;
        m--;
    }
    head=node.next;
    node.next=null;
    return head;
};
 /* var rotateRight = function(head, k) {
    if(head==null || head.next==null || k==0){
        return head;
    }
    // 快慢指针
    let newhead=new ListNode();    // 添加头结点
    newhead.next=head;
    let fast=newhead,slow=newhead;
    for(let i=0;i<k;i++){
        fast=fast.next;
        if(fast==null){
            fast=head;
        }
    }
    while(fast.next!=null){
        fast=fast.next;
        slow=slow.next;        // 找到尾节点
    }
    if(slow!=newhead){
        head=slow.next;            //变更头节点
        fast.next=newhead.next;    // 首尾相接
        slow.next=null;                // 断开环
    }
    return head;
}; */

let arr=[1,2,3,4,5,6];
let ko=list(arr);
console.log(rotateRight(ko,2));